package aboutme.felipesilvestre.oracaolista.util;

import java.util.List;

import aboutme.felipesilvestre.oracaolista.OracaoActivity;
import aboutme.felipesilvestre.oracaolista.R;
import aboutme.felipesilvestre.oracaolista.vo.Oracao;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class OracaoAdapter extends BaseAdapter {

	private List<Oracao> oracoes;
	private Activity activity;
	private OracaoAdapter instance;

	public OracaoAdapter(Activity act, List<Oracao> oracoes) {
		this.oracoes = oracoes;
		this.activity = act;
		instance = this;
	}

	@Override
	public boolean isEnabled(int position) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean areAllItemsEnabled() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int getCount() {

		return oracoes.size();
	}

	@Override
	public Oracao getItem(int arg0) {

		return oracoes.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {

		return arg0;
	}

	@Override
	public View getView(final int arg0, View arg1, ViewGroup arg2) {

		final Oracao oracao = oracoes.get(arg0);
		LayoutInflater inflater = (LayoutInflater) activity.getBaseContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.itemoracao, null);

		ImageView imgCateg = (ImageView) v.findViewById(R.id.imgItemOracao);
		imgCateg.setImageDrawable(oracao.getImageCategoria());

		TextView txtTitulo = (TextView) v
				.findViewById(R.id.txtTituloItemOracao);
		if (oracao.getTitulo().length() > 20)
			txtTitulo.setText(oracao.getTitulo().substring(0, 20) + "...");
		else
			txtTitulo.setText(oracao.getTitulo());

		TextView txtDesc = (TextView) v.findViewById(R.id.txtDescItemOracao);
		if (oracao.getDescricao().length() > 30)
			txtDesc.setText(oracao.getDescricao().substring(0, 30) + "...");
		else
			txtDesc.setText(oracao.getDescricao());
		
		
		ImageButton imgRemove = (ImageButton) v.findViewById(R.id.imgBtRemoveOracao);
		imgRemove.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			
				Toast.makeText(activity.getBaseContext(), "oracao eliminada " + oracao.getTitulo(), Toast.LENGTH_LONG).show();
				oracoes.remove(arg0);
				notifyDataSetChanged();
				
			}
		});

		v.setOnClickListener(new OnClickListener() {
			private int pos = arg0;

			@Override
			public void onClick(View v) {
				Toast.makeText(activity.getBaseContext(), "Click-" + String.valueOf(pos) + " oracao" + oracao.getTitulo(),
						Toast.LENGTH_SHORT).show();
				Intent intent = new Intent(activity.getBaseContext(), OracaoActivity.class);
				
				intent.putExtra("id",oracao.getId());
				intent.putExtra("titulo",oracao.getTitulo());
				intent.putExtra("desc",oracao.getDescricao());
				intent.putExtra("datacriacao",oracao.getDataCriacao());
				intent.putExtra("categoria",oracao.getCategoria());
				
				intent.putExtra("novo", false);
				
				activity.startActivityForResult(intent, 10);
			}
		});

		return v;
	}

}
